# -*- coding:utf-8 -*-

import re
import urllib2
import string
import time
import json
import os

macPATH = "/Users/jacob/python/data/3.json"
PATH = "D:\\lightSoftware\\lightSoftware\\python\\3.json"
purl = "http://www.zzgcjyzx.com"
#pageurl = "http://www.zzgcjyzx.com/zzztb/gcxx/002001/002001001/"
pageurl = "http://www.zzgcjyzx.com/zzztb/gcxx/002001/002001001/MoreInfo.aspx?CategoryNum=002001001"

page = urllib2.urlopen(pageurl).read()

#origin List
#projectList = re.findall(r"<td width='20'><img src='/zzztb/images/dots/dot1.gif' border=0></td>(.*?)<td width='80'>", page, re.S)

projectList = re.findall(r'<img src="/zzztb/images/dots/dot1.gif">\r\n\t\t\t\t\t\t</td><td align="left" valign="middle">\r\n\t\t\t\t\t\t\t(.*?)\r\n\t\t\t\t\t\t</td><td align="center" valign="middle" width="80">', page, re.S)


dateList = []

#ProList
proList = []

#supervisor = '\xe7\x9b\x91\xe7\x90\x86'
supervisor = "招标"#.decode("GBK")
#designer = '\xe8\xae\xbe\xe8\xae\xa1'.decode("GBK")
designer = "设计"#.decode("GBK")
#investigation = '\xe5\x8b\x98\xe5\xaf\x9f'.decode("GBK")
investigation = "勘察"#.decode("GBK")

def isUnrelated(title):

	o = title.find(supervisor)
	p = title.find(designer)
	q = title.find(investigation)

	r = o + p + q 
	if r < -2:
		return False
	else:
		return True









print len(projectList)


for index, item in enumerate(projectList):
	url = purl + ''.join(re.findall(r'<a href="(.*?)" target="_blank"', item, re.S))
	title = ''.join(re.findall(r'target="_blank" title=".*?">(.*?)</a>', item, re.S)).decode("GBK")
	date = ''.join(re.findall(r"<td width=\"80\">(.*?)</td>", item, re.S))
	print index
	project = {
		"url": url,
		"title": title,
		"date": date
	}
	proList.append(project)



#print len(proList)


jsProjectList = json.dumps(proList, indent=4, sort_keys=True)


#print jsProjectList
with open(macPATH, 'w') as f:
	json.dump(proList, f, indent=4, sort_keys=True)


for index, item in enumerate(proList):

	title = item["title"].encode("GBK")
	print title
	yOsN = isUnrelated(title)
	print yOsN


































count = len(proList)

project = proList[0]

thisUrl = project["url"]

print thisUrl

try:

	thisPage = urllib2.urlopen(thisUrl).read()
except urllib2.URLError, e:
	if hasattr(e, "code"):
		print "The Server couldn't fulfill the request."
		print "Error Code: %s" % e.code
	elif hasattr(e, "reason"):
		print "We failed to reach a server. Please check your url and read the Reason."
		print "Reason: %s" % e.reason

#print thisPaget
"""
b = "\xd4\xb0\xc1\xd6\xc2\xcc\xbb\xaf"
a = re.findall(r"%s" % b, thisPage, re.S)


print len(a)
"""

#for index, item in enumerate(proList):

	

	





















